<template>
  <div class="admin-layout">
    <el-container>
      <el-aside width="200px">
        <div class="admin-sidebar">
          <div class="sidebar-header">
            <div class="logo-circle">
              <img src="@/assets/logo.png" alt="logo" class="logo-img" />
            </div>
            <span class="title">管理后台</span>
          </div>
          <el-menu
            router
            :default-active="$route.path"
            class="admin-menu"
            background-color="#2d3a4b"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
          >
            <el-menu-item index="/admin/medicines">
              <el-icon><FirstAidKit /></el-icon>
              <span>药品管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/orders">
              <el-icon><List /></el-icon>
              <span>订单管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/users">
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/articles">
              <el-icon><Document /></el-icon>
              <span>养生文章管理</span>
            </el-menu-item>
            <el-menu-item index="/admin/doctors">
              <el-icon><UserFilled /></el-icon>
              <span>在线医生管理</span>
            </el-menu-item>
          </el-menu>
        </div>
      </el-aside>
      
      <el-container>
        <el-header class="admin-header">
          <div class="header-right">
            <span class="username">{{ username }}</span>
            <el-button type="text" class="logout-btn" @click="handleCommand('logout')">
              <el-icon><SwitchButton /></el-icon>
              退出
            </el-button>
          </div>
        </el-header>
        <el-main class="admin-main">
          <router-view :key="$route.fullPath"></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStore } from 'vuex'
import { Plus, FirstAidKit, List, User, Document, SwitchButton, UserFilled } from '@element-plus/icons-vue'

export default {
  name: 'AdminLayout',
  setup() {
    const router = useRouter()
    const route = useRoute()
    const store = useStore()
    const username = computed(() => store.state.user?.username || '管理员')

    const currentPath = computed(() => {
      const pathMap = {
        '/admin/medicines': '药品管理',
        '/admin/orders': '订单管理',
        '/admin/users': '用户管理',
        '/admin/articles': '养生文章管理'
      }
      return pathMap[route.path] || '首页'
    })

    const handleCommand = (command) => {
      if (command === 'logout') {
        localStorage.removeItem('token')
        localStorage.removeItem('role')
        localStorage.removeItem('username')
        localStorage.removeItem('userId')
        store.commit('SET_USER', null)
        router.push('/login')
      }
    }

    return {
      username,
      currentPath,
      handleCommand,
      User,
      SwitchButton,
      UserFilled
    }
  }
}
</script>

<style scoped>
.admin-layout {
  min-height: 100vh;
  background: #f5f6fa;
}

.admin-sidebar {
  height: 100vh;
  background: #2d3a4b;
  position: fixed;
  width: 200px;
  box-shadow: 1px 0 8px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  z-index: 10;
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
  transition: width 0.3s;
  border-right: none;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  padding: 0 18px;
  color: #fff;
  background: #22304a;
  border-bottom: 1px solid #22304a;
  font-size: 20px;
  font-weight: bold;
  letter-spacing: 1px;
  margin-bottom: 0;
}

.sidebar-header .logo-circle {
  width: 36px;
  height: 36px;
  background: #409EFF;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  box-shadow: 0 1px 4px rgba(64,158,255,0.08);
}

.sidebar-header .el-icon {
  font-size: 22px;
  color: #fff;
}

.sidebar-header .title {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
}

.admin-menu {
  border: none;
  background: transparent;
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  height: calc(100vh - 70px);
}

.admin-menu .el-menu-item {
  display: flex;
  align-items: center;
  height: 36px;
  border-radius: 6px;
  margin: 0 10px 2px 10px;
  transition: background 0.2s, color 0.2s, font-weight 0.2s;
  font-size: 16px;
  font-weight: 500;
  color: #bfcbd9 !important;
  position: relative;
  padding-left: 20px !important;
  background: transparent !important;
}

.admin-menu .el-menu-item.is-active,
.admin-menu .el-menu-item:hover {
  background: #22304a !important;
  color: #409EFF !important;
  font-weight: 700;
}

.admin-menu .el-menu-item.is-active i,
.admin-menu .el-menu-item:hover i {
  color: #409EFF;
  transform: scale(1.08);
}

.admin-menu .el-menu-item i {
  margin-right: 10px;
  font-size: 18px;
  transition: color 0.2s, transform 0.2s;
  color: #bfcbd9;
}

.admin-header {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 60px;
  padding: 0 32px 0 0;
  border-bottom: 1px solid #f0f0f0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.username {
  color: #222;
  font-size: 15px;
  font-weight: 500;
}

.logout-btn {
  color: #ff4d4f;
  font-weight: bold;
  font-size: 15px;
  border-radius: 4px;
  padding: 6px 18px;
  border: none;
  background: none;
  transition: color 0.2s, background 0.2s;
}
.logout-btn:hover {
  color: #fff;
  background: #ff4d4f;
}

.admin-main {
  background: transparent;
  padding: 32px 16px 32px 0px;
  min-height: 100vh;
  transition: padding 0.3s;
  position: relative;
}

.admin-main > * {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(64,158,255,0.06);
  padding: 18px 16px 16px 16px;
  margin-bottom: 18px;
  position: relative;
  z-index: 2;
}

.admin-main .page-title {
  font-size: 20px;
  font-weight: 700;
  color: #409EFF;
  margin-bottom: 18px;
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: 0.5px;
}

@media (max-width: 900px) {
  .admin-sidebar {
    width: 54px;
    min-width: 54px;
    padding: 0;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
  }
  .admin-main {
    padding: 12px 2px 12px 64px;
  }
  .sidebar-header {
    font-size: 12px;
    padding: 0 4px;
  }
  .admin-menu .el-menu-item {
    font-size: 12px;
    margin: 0 2px 4px 2px;
    padding-left: 10px !important;
    height: 32px;
  }
}

.logo-img {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  display: block;
}
</style> 